import {observable,action} from 'mobx';
import * as DoorPageAPI from '../../apis/task/doorPage'
import * as taskMain from '../../apis/task/taskMain'
import {WeaTableNew } from 'comsMobx';
const { TableStore } = WeaTableNew;
export default class TaskMainStores {
    @observable addname = '';//快速创建任务名称
    excludeids = '';//新增任务id集合
    @observable datalist = [];//数据列表
    pagenum = 1;//层级和列表视图当前页数
    itotal = 1;//层级和列表视图总条数
    @observable hasmore ="";//层级和列表模式下是否有更多
    pagesize = 20; //列表每页页数
    @observable refreshlist = true;
    @observable sorttype = "1";//视图
    condtype ="1";//任务分类
    @observable datetype=[];//分类视图类型数组
    @observable orderby = ""; //排序
    @observable orderbytype="desc";
    @observable taskname = "";//任务名称
    optparams = {};//ViewTree组件操作数组
    localtime = new Date().getTime();
    @observable loading = false;
    @observable slidevisible = false;//侧边弹出框显隐
    @observable currentstatus = "1";//当前状态
    @observable principalid = [];//高级搜索列表责任人
    @observable begindate = "";//高级搜索列表开始日期
    @observable begindate1 = "";//高级搜索列表开始日期
    @observable enddate = "";//高级搜索列表结束日期
    @observable enddate1 = "";//高级搜索列表结束日期
    @observable lev = "";//高级搜索列表紧急程度
    @observable tag = "";//高级搜索标签
    @observable hrmid = [];
    viewId = "0";//已保存视图id
    isOutSearch = false;//是否是外部跳转过来
    reportstatus = "";//外部传进来的状态
    personType = "";//外部传进来的personType
    cstartdate = "";//外部传进来的创建开始时间
    cenddate = "";//外部传进来的创建结束时间
    cuserid = "";//外部传进来的人员id
    @observable dropdisplay = true;
    @observable checkedIds = '';
    @observable allCheckBoxVal = "0";

    constructor() {

    }
    //添加任务
    @action
    addTask = params => {
        params.mainpage = "1";
        params.sorttype = this.sorttype;
        taskMain.addTask(params).then(action((data) => {
            if(data.status==1){
                this.refreshlist = false;
                this.optparams = {};
                this.optparams.datas = data.datas;
                this.optparams.opttype = "add";
                this.optparams.optkey = data.taskId;
                this.localtime = this.getLocalTime();
                if(this.sorttype==1 || this.sorttype ==4){
                    this.addname ="";
                }else{
                    this.datetype[params.index].hasdata = "1";
                    this.datetype[params.index].inputval = "";
                }
                this.excludeids += data.taskId+",";
                this.refreshlist = true;
                if (params.doback) {
                    params.doback(data.taskId);
                }
            }
        }));
    }
    //得到任务列表数据
    @action
    getTaskList = params => {
        this.dropdisplay = true;
        this.allCheckBoxVal = "0";
        this.getCommonParams(params);
        this.optparams = {};
        this.excludeids = "";
        this.addTaskView({});
        this.slidevisible = false;

        if (this.sorttype == "1" && this.dataShowType == "2") {
            this.getTableListData(params)
        } else {
            this.loading = true;
            taskMain.getTaskList(params).then(action((data) => {
                if(data.status){
                    if(this.sorttype==1 || this.sorttype==4){
                        this.hasmore = data.hasmore;
                        this.itotal = data.total;
                        this.pagenum = 1;
                    }
                    this.datetype = data.datetype;
                    this.datalist = data.datas;
                }
                this.loading = false;
            }));
        }
    }
    //更多列表
    @action
    getTaskMoreList = (params) =>{
        this.loading = true;
        this.getCommonParams(params);
        params.excludeids = this.excludeids;
        taskMain.getMoreTaskList(params).then(action(res => {
            if(res.status){
              if(this.sorttype==1 || this.sorttype==4){
                this.pagenum = params.pagenum;
                this.hasmore = res.hasmore;
              }else{
                 this.datetype[params.index].pagenum = params.pagenum;
                 this.datetype[params.index].hasmore = res.hasmore;
              }
              this.datalist = this.datalist.concat(res.datas);
            }
            this.loading = false;
        }));
    }
    @action
    getCommonParams(params){
        params.sorttype = this.sorttype;
        params.orderby = this.orderby;
        params.orderbytype = this.orderbytype;
        params.taskname = this.taskname;
        params.pagesize = this.pagesize;
        if(this.sorttype!=1){
            params.pagesize = 10;
        }
        if(this.principalid && this.principalid.length>0){
            params.principalid = this.principalid[0].id;
        }
        params.enddate = this.enddate;
        params.enddate1 = this.enddate1;
        if(this.isOutSearch){
            params.condtype = 0;
            params.status = this.reportstatus;
            params.personType = this.personType;
            params.cstartdate = this.cstartdate;
            params.cenddate = this.cenddate;
            params.cuserid = this.cuserid;
        }else{
            params.condtype = this.condtype;
            params.status = this.currentstatus;
        }
        if(this.hrmid && this.hrmid.length>0){
            params.hrmid = this.hrmid[0].id;
        }
        if(this.tag && this.tag.length>0){
            // params.tag = this.tag[0].id;
            params.tag = this.tag;
        }
        params.begindate = this.begindate;
        params.begindate1 = this.begindate1;
        params.lev = this.lev;
    }
    //添加或取消关注
    @action
    setSpecial = params => {
        DoorPageAPI.setSpecial(params).then(action((data) => {
            if(data.status==1){
                this.refreshlist = false;
                let comsbtn = params.comsbtn;
                let arr = data.smap;
                comsbtn.map((element,idx)=>{
                    if(element.type==params.type){
                        comsbtn[idx] = arr;
                    }
                });
                this.optparams = {};
                this.optparams.comsbtn = comsbtn;
                this.optparams.opttype = "update";
                this.optparams.optkey = params.taskid;
                if(params.doback){
                    params.doback();
                }
                this.localtime = this.getLocalTime();
                this.refreshlist = true;
            }
        }));
    }
    //修改任务状态
    @action
    toChangeStatus = params => {
        DoorPageAPI.toChangeStatus(params).then(action((data) => {
            if(data.status==1 ){
                this.refreshlist = false;
                if(params.status==4 || (this.currentstatus==1 && params.status==2) || (this.currentstatus==2 && params.status==1)){
                    this.optparams = {};
                    this.optparams.opttype = "del";
                    this.optparams.optkey = params.taskId;
                }else{
                    let comsbtn = params.comsbtn;
                    let arr = data.smap;
                    comsbtn.map((element,idx)=>{
                        if(element.type==params.type){
                            comsbtn[idx] = arr;
                        }
                    });
                    this.optparams = {};
                    this.optparams.comsbtn = comsbtn;
                    this.optparams.opttype = "update";
                    this.optparams.optkey = params.taskId;
                    this.optparams.status = data.newstatus || params.status;
                }
                if(params.doback){
                    params.doback();
                }
                this.localtime = this.getLocalTime();
                this.refreshlist = true;
            }
        }));
    }
    //得到子任务列表数据
    @action
    getChildTaskList = params => {
        this.loading = true;
        taskMain.getChildTaskList(params).then(action((data) => {
            if(data.status==1){
                this.refreshlist = false;
                this.optparams ={};
                this.optparams.datas = data.datas;
                this.optparams.opttype = "childs";
                this.optparams.optkey = params.taskid;
                this.localtime = this.getLocalTime();
                this.refreshlist = true;
            }
            this.loading = false;
        }));
    }
    @action
    addTaskView = params =>{//增加任务记住视图
        params.condtype = this.condtype;
        params.status = this.currentstatus;
        params.sorttype = this.sorttype;
        params.showtype = this.dataShowType
        params.viewId = this.viewId;
        taskMain.addTaskView(params).then(action((data) => {

        }));
    }
    @action
    setNewTaskInfo(params){//新增任务编辑任务返回设置值
        this.refreshlist = false;
        this.optparams = params;
        if(params.opttype=='add' || params.opttype=='dialogAdd'){
            this.excludeids += params.optkey +",";
            if(params.optdatetype && this.datetype && this.datetype.length>0){
                for(let i=0;i<this.datetype.length;i++){
                    if(this.datetype[i].id == params.optdatetype){
                        this.datetype[i].hasdata = "1";
                    }
                }
            }
        }
        this.localtime = this.getLocalTime();
        this.refreshlist = true;
    }
    @action
    getLocalTime(){
        return new Date().getTime();
    }
    @action("标签处理")
    editTaskTag = params =>{
        this.loading = true;
        taskMain.editTaskInfo(params).then(action((data) => {
            if(data.status==1 && params.doback){
                params.doback(data.status);
            }
            if(params.tagtype=="2" || params.tagtype=="3"){
                this.getTaskList({});
            }
            this.loading = false;
        }))
    }
    @action("批量删除")
    batchDelTask = params =>{
        this.loading = true;
        params.operation = "batchDel";
        DoorPageAPI.toChangeStatus(params).then(action((data) => {
            this.getTaskList({});
            if(params.doback){
                params.doback(data.status);
            }
            this.loading = false;
        }))
    }
    @action("重置dropdisplay")
    setDropDisplay(isSelect){
        if(isSelect){
            this.dropdisplay = false;
        }else{
            this.dropdisplay = true;
        }
    }
    @action("设置全选CheckBox框的值")
    setAllCheckBoxValue(v,currentid){
        if(currentid && currentid!=""){
            this.datetype && this.datetype.length>0 &&  this.datetype.map((element,idx)=>{
                if(element.id == currentid){
                    element.allCheckBoxVal = v;
                }
            })
        }else{
            this.allCheckBoxVal = v;
        }
    }

    isBatchDel = true    // 是否处于批量删除

    @observable
    dataShowType = "1"   // 1 -- 分类展示   2 -- 表格展示
    
    @action
    changeDataShowType = (dataShowType) => {
        this.dataShowType = dataShowType
    }

    @observable
    tableStore = new TableStore()

    @action
    getTableListData = (params = {}) => {
        params.sorttype = ""
        params.isBatchDel = this.isBatchDel ? "1" : "0"
        taskMain.tableList(params).then(res => {
            if (res.status) {
                this.tableStore.getDatas(res.datas, params.isReloadCurrent ? this.tableStore.current : 1)
            }
        })
    }

    @observable
    tableSelectedRowKeys = [] 

}